package net.wanho.zaixian.course.controller;

import com.alibaba.fastjson.JSONObject;
import net.wanho.zaixian.course.service.CourseBaseService;
import net.wanho.zaixian.course.service.CoursePicService;
import net.wanho.zaixian.pojo.course.CourseBase;
import net.wanho.zaixian.pojo.course.CoursePic;
import net.wanho.zaixian.pojo.course.ext.CourseInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class CourseController {

    @Resource
    private CourseBaseService courseBaseService;

    @Resource
    private CoursePicService coursePicService;

    /**
     * 查询所以课程
     * @param page
     * @param size
     * @return
     */

    @GetMapping("/course/coursebase/list/{page}/{size}")
    public JSONObject getAllCourseBase(@PathVariable(name = "page") int page, @PathVariable(name = "size") int size) {

        Pageable pageable = PageRequest.of(page-1,size);
        List<CourseBase> courseBaseList = courseBaseService.findAllByPage(pageable);
        List<CourseBase> all = courseBaseService.findAll();
        List<CourseInfo> courseInfoList = new ArrayList<>();
        for(CourseBase courseBase:courseBaseList){
            CourseInfo courseInfo = new CourseInfo();
            BeanUtils.copyProperties(courseBase,courseInfo);

            List<CoursePic> coursePicList = coursePicService.findCoursePicByCourseId(courseBase.getId());
            List<String> pic = new ArrayList<>();
            for(CoursePic coursePic: coursePicList){
                pic.add(coursePic.getPic());
            }
            courseInfo.setPic(pic);
            courseInfoList.add(courseInfo);
        }

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("list",courseInfoList);
        jsonObject.put("total",all.size());
        jsonObject.put("success",true);
        return jsonObject;

    }



    /**
     * 增加课程
     * @param courseBase
     * @return
     */
    @PostMapping("/course/coursebase/add")
    public JSONObject allCourse(@RequestBody CourseBase courseBase){

        JSONObject jsonObject = new JSONObject();
        try {
            courseBaseService.addCourseBase(courseBase);
            jsonObject.put("success",true);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("success",false);
            return jsonObject;
        }

    }

    /**
     * 根据Id查询courseBase
     * @param id
     * @return
     */
    @GetMapping("/course/coursebase/fetch/{id}")
    public CourseBase getCourseBaseById(@PathVariable(name = "id") String id){
        CourseBase courseBase = courseBaseService.getCourseBaseById(id);
        return courseBase;

    }

    /**
     * 跟新课程
     * @param courseBase
     * @return
     */
    @PostMapping("/course/coursebase/update")
    public JSONObject updateCourseBase(@RequestBody CourseBase courseBase){
        JSONObject jsonObject = new JSONObject();
        try {
            courseBaseService.updateCourseBase(courseBase);
            jsonObject.put("success",true);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("success",false);
            return jsonObject;
        }
    }




}

